package com.woniuxy.bookmallmanagersrever.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmallmanagersrever.entity.TBook;
import com.woniuxy.bookmallmanagersrever.mapper.TBookMapper;
import com.woniuxy.bookmallmanagersrever.vo.BookVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookService {
    public static final String ES_EXCHANGE = "es_exchange";
    public static final String ES_ROUTING_KEY = "es_routing_key";
    @Autowired
    TBookMapper bookMapper ;

    @Autowired
    RabbitTemplate rabbitTemplate ;

    public PageInfo<BookVo> findBookByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<BookVo> books = bookMapper.selectBookByCons(name);
        return new PageInfo<>(books);
    }

    public void add(TBook book) {
        book.setCreatetime(new Date());
        book.setBuycount(0);
        book.setCollectioncount(0);
        book.setReadcount(0);
        book.setState(1);

        bookMapper.insert(book);

        //同步索引库   发送一个MQ消息
        Map<String,Object> maps = new HashMap<>();
        maps.put("bookId",book.getId());
        //发送消息
        rabbitTemplate.convertAndSend(ES_EXCHANGE,ES_ROUTING_KEY,maps);
    }
}
